//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "MultiStepTarn.h"
using namespace Cephei::QL::Models::Marketmodels::Products::Multistep;
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/MarketModelMultiProductCashFlow.h>
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/Products/MultiProductMultiStep.h>
using namespace Cephei::QL::Models::Marketmodels;
using namespace Cephei::QL::Models::Marketmodels::Products;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::CMultiStepTarn (Cephei::Core::IVector<Double>^ rateTimes, Cephei::Core::IVector<Double>^ accruals, Cephei::Core::IVector<Double>^ accrualsFloating, Cephei::Core::IVector<Double>^ paymentTimes, Cephei::Core::IVector<Double>^ paymentTimesFloating, Double totalCoupon, Cephei::Core::IVector<Double>^ strikes, Cephei::Core::IVector<Double>^ multipliers, Cephei::Core::IVector<Double>^ floatingSpreads) : CMultiProductMultiStep(CMultiStepTarn::typeid)
{
    try
    {
#ifdef HANDLE
        _phMultiStepTarn = NULL;
#endif
        rateTimes->Lock();
        INativeVector<Double>^ _NCIrateTimes = rateTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateTimes = safe_cast<CDoubleVector^>(_NCIrateTimes);
        std::vector<QuantLib::Time>& _rateTimes = static_cast<std::vector<QuantLib::Time>&> (_NCrateTimes->GetReference ());
        accruals->Lock();
        INativeVector<Double>^ _NCIaccruals = accruals->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCaccruals = safe_cast<CDoubleVector^>(_NCIaccruals);
        std::vector<QuantLib::Real>& _accruals = static_cast<std::vector<QuantLib::Real>&> (_NCaccruals->GetReference ());
        accrualsFloating->Lock();
        INativeVector<Double>^ _NCIaccrualsFloating = accrualsFloating->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCaccrualsFloating = safe_cast<CDoubleVector^>(_NCIaccrualsFloating);
        std::vector<QuantLib::Real>& _accrualsFloating = static_cast<std::vector<QuantLib::Real>&> (_NCaccrualsFloating->GetReference ());
        paymentTimes->Lock();
        INativeVector<Double>^ _NCIpaymentTimes = paymentTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCpaymentTimes = safe_cast<CDoubleVector^>(_NCIpaymentTimes);
        std::vector<QuantLib::Time>& _paymentTimes = static_cast<std::vector<QuantLib::Time>&> (_NCpaymentTimes->GetReference ());
        paymentTimesFloating->Lock();
        INativeVector<Double>^ _NCIpaymentTimesFloating = paymentTimesFloating->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCpaymentTimesFloating = safe_cast<CDoubleVector^>(_NCIpaymentTimesFloating);
        std::vector<QuantLib::Time>& _paymentTimesFloating = static_cast<std::vector<QuantLib::Time>&> (_NCpaymentTimesFloating->GetReference ());
        QuantLib::Real _totalCoupon = (QuantLib::Real)ValueHelper::Convert (totalCoupon); //d
        strikes->Lock();
        INativeVector<Double>^ _NCIstrikes = strikes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCstrikes = safe_cast<CDoubleVector^>(_NCIstrikes);
        std::vector<QuantLib::Real>& _strikes = static_cast<std::vector<QuantLib::Real>&> (_NCstrikes->GetReference ());
        multipliers->Lock();
        INativeVector<Double>^ _NCImultipliers = multipliers->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCmultipliers = safe_cast<CDoubleVector^>(_NCImultipliers);
        std::vector<QuantLib::Real>& _multipliers = static_cast<std::vector<QuantLib::Real>&> (_NCmultipliers->GetReference ());
        floatingSpreads->Lock();
        INativeVector<Double>^ _NCIfloatingSpreads = floatingSpreads->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfloatingSpreads = safe_cast<CDoubleVector^>(_NCIfloatingSpreads);
        std::vector<QuantLib::Real>& _floatingSpreads = static_cast<std::vector<QuantLib::Real>&> (_NCfloatingSpreads->GetReference ());
        _ppMultiStepTarn = new boost::shared_ptr<QuantLib::MultiStepTarn> (new QuantLib::MultiStepTarn ( _rateTimes,  _accruals,  _accrualsFloating,  _paymentTimes,  _paymentTimesFloating,  _totalCoupon,  _strikes,  _multipliers,  _floatingSpreads ));
        SetMultiProductMultiStep (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepTarn));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (rateTimes != nullptr) rateTimes->Unlock();    //not optional
        if (accruals != nullptr) accruals->Unlock();    //not optional
        if (accrualsFloating != nullptr) accrualsFloating->Unlock();    //not optional
        if (paymentTimes != nullptr) paymentTimes->Unlock();    //not optional
        if (paymentTimesFloating != nullptr) paymentTimesFloating->Unlock();    //not optional
        if (strikes != nullptr) strikes->Unlock();    //not optional
        if (multipliers != nullptr) multipliers->Unlock();    //not optional
        if (floatingSpreads != nullptr) floatingSpreads->Unlock();    //not optional
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::CMultiStepTarn (boost::shared_ptr<QuantLib::MultiStepTarn>& childNative, Object^ owner) : CMultiProductMultiStep(CMultiStepTarn::typeid)
{
#ifdef HANDLE
	_phMultiStepTarn = NULL;
#endif
	_ppMultiStepTarn = &childNative;
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepTarn));
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::CMultiStepTarn (QuantLib::MultiStepTarn& childNative, Object^ owner) : CMultiProductMultiStep(CMultiStepTarn::typeid)
{
#ifdef HANDLE
	_phMultiStepTarn = NULL;
#endif
	_ppMultiStepTarn = new boost::shared_ptr<QuantLib::MultiStepTarn> (&childNative);
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepTarn));
    _MultiStepTarnOwner = owner;
    _MultiProductMultiStepOwner = owner;
}

Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::CMultiStepTarn (CMultiStepTarn^ copy) : CMultiProductMultiStep(CMultiStepTarn::typeid)
{
#ifdef HANDLE
	_phMultiStepTarn = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMultiStepTarn = new boost::shared_ptr<QuantLib::MultiStepTarn> (copy->GetShared());
        _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepTarn));
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::CMultiStepTarn (PLATFORM::Type^ t) : CMultiProductMultiStep(CMultiStepTarn::typeid)
{
#ifdef HANDLE
	_phMultiStepTarn = NULL;
#endif
	if (!t->IsSubclassOf(CMultiStepTarn::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::CMultiStepTarn (QuantLib::Handle<QuantLib::MultiStepTarn>& childNative, Object^ owner)  : CMultiProductMultiStep(CMultiStepTarn::typeid)
{
	_phMultiStepTarn = &childNative;
	_ppMultiStepTarn = &static_cast<boost::shared_ptr<QuantLib::MultiStepTarn>>(childNative.currentLink());
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepTarn));
    _MultiStepTarnOwner = owner;
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::CMultiStepTarn (QuantLib::Handle<QuantLib::MultiStepTarn> childNative)  : CMultiProductMultiStep(CMultiStepTarn::typeid)
{
	_phMultiStepTarn = &childNative;
	_ppMultiStepTarn = &static_cast<boost::shared_ptr<QuantLib::MultiStepTarn>>(childNative.currentLink());
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepTarn));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::CMultiStepTarn (QuantLib::MultiStepTarn childNative)  : CMultiProductMultiStep(CMultiStepTarn::typeid)
{
#ifdef HANDLE
	_phMultiStepTarn = NULL;
#endif
	_ppMultiStepTarn = new boost::shared_ptr<QuantLib::MultiStepTarn> (new QuantLib::MultiStepTarn (childNative));
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepTarn));
}
#endif

Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::~CMultiStepTarn ()
{
    if (_ppMultiStepTarn != NULL)
    {
	    delete _ppMultiStepTarn;
        _ppMultiStepTarn = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::!CMultiStepTarn ()
{
    if (_ppMultiStepTarn != NULL)
    {
	    delete _ppMultiStepTarn;
    }
}
QuantLib::MultiStepTarn& Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::GetReference ()
{
    if (_ppMultiStepTarn == NULL) throw REFNEW NativeNullException ();
	return **_ppMultiStepTarn;
}
boost::shared_ptr<QuantLib::MultiStepTarn>& Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::GetShared ()
{
    if (_ppMultiStepTarn == NULL) throw REFNEW NativeNullException ();
	return *_ppMultiStepTarn;
}
QuantLib::MultiStepTarn* Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::GetPointer ()
{
    if (_ppMultiStepTarn == NULL) throw REFNEW NativeNullException ();
	return &**_ppMultiStepTarn;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MultiStepTarn>& Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::GetHandle ()
{
	if (_phMultiStepTarn == NULL)
	{
		_phMultiStepTarn = new Handle<QuantLib::MultiStepTarn> (*_ppMultiStepTarn);
	}
	return *_phMultiStepTarn;
}
#endif
bool Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::HasNative () 
{
	return (_ppMultiStepTarn != NULL);
}

UInt64 Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::MaxNumberOfCashFlowsPerProductPerStep::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMultiStepTarn)->maxNumberOfCashFlowsPerProductPerStep ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::NumberOfProducts::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMultiStepTarn)->numberOfProducts ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::PossibleCashFlowTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppMultiStepTarn)->possibleCashFlowTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::IMultiStepTarn^ Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppMultiStepTarn)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn::NextTimeStep (Cephei::QL::Models::Marketmodels::ICurveState^ currentState, Cephei::Core::IVector<UInt64>^ numberCashFlowsThisStep, Cephei::Core::IMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ cashFlowsGenerated)
{
    CCurveState^ _CcurrentState;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CcurrentState = safe_cast<CCurveState^> (currentState);
        _CcurrentState->Lock();
        QuantLib::CurveState& _currentState = static_cast<QuantLib::CurveState&> (_CcurrentState->GetReference ()); 
        numberCashFlowsThisStep->Lock();
        INativeVector<UInt64>^ _NCInumberCashFlowsThisStep = numberCashFlowsThisStep->getFeature (NativeFeature::Value);
        CUInt64Vector^ _NCnumberCashFlowsThisStep = safe_cast<CUInt64Vector^>(_NCInumberCashFlowsThisStep);
        std::vector<QuantLib::Size>& _numberCashFlowsThisStep = static_cast<std::vector<QuantLib::Size>&> (_NCnumberCashFlowsThisStep->GetReference ());
        cashFlowsGenerated->Lock();
        INativeMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ _NCIcashFlowsGenerated = cashFlowsGenerated->getFeature (NativeFeature::Value);
        CMarketModelMultiProductCashFlowMatrix^ _NCcashFlowsGenerated = safe_cast<CMarketModelMultiProductCashFlowMatrix^>(_NCIcashFlowsGenerated);
        std::vector<std::vector<QuantLib::MarketModelMultiProductCashFlow> >& _cashFlowsGenerated = static_cast<std::vector<std::vector<QuantLib::MarketModelMultiProductCashFlow> >&> (_NCcashFlowsGenerated->GetReference ());
    	bool _rv = (bool)(*_ppMultiStepTarn)->nextTimeStep ( _currentState,  _numberCashFlowsThisStep,  _cashFlowsGenerated );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcurrentState != nullptr) _CcurrentState->Unlock();
        if (numberCashFlowsThisStep != nullptr) numberCashFlowsThisStep->Unlock(); //NO2  
        if (cashFlowsGenerated != nullptr) cashFlowsGenerated->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Products::Multistep::IMultiStepTarn^ Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepTarn_Factory::Create (Cephei::Core::IVector<Double>^ rateTimes, Cephei::Core::IVector<Double>^ accruals, Cephei::Core::IVector<Double>^ accrualsFloating, Cephei::Core::IVector<Double>^ paymentTimes, Cephei::Core::IVector<Double>^ paymentTimesFloating, Double totalCoupon, Cephei::Core::IVector<Double>^ strikes, Cephei::Core::IVector<Double>^ multipliers, Cephei::Core::IVector<Double>^ floatingSpreads)
{
    return REFNEW CMultiStepTarn ( rateTimes,  accruals,  accrualsFloating,  paymentTimes,  paymentTimesFloating,  totalCoupon,  strikes,  multipliers,  floatingSpreads);
}
